from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

cancer = load_breast_cancer()  # 乳腺癌数据集
# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(
    cancer.data, cancer.target, test_size=0.2)

# 构建模型
model = LogisticRegression(max_iter=5000)  # 最大迭代次数 max_iter
model.fit(X_train, y_train)  # 训练模型

y_predict = model.predict(X_test)
accuracy_score_value = accuracy_score(y_test, y_predict)  # 准确率
recall_score_value = recall_score(y_test, y_predict)  # 召回率
precision_score_value = precision_score(y_test, y_predict)  # 精确率
classification_report_value = classification_report(y_test, y_predict)  # 混淆矩阵
print("准确率：", accuracy_score_value)
print("召回率：", recall_score_value)
print("精确率：", precision_score_value)
print("分类性能报告：\n",classification_report_value)

# 3）特征工程：标准化
# transfer = StandardScaler()
# x_train = transfer.fit_transform(X_train)
# x_test = transfer.transform(X_test)
# 5）模型评估
# 方法1：直接比对真实值和预测值
y_predict = model.predict(X_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)

# 方法2：计算准确率
score = model.score(X_test, y_test)
print("准确率为：\n", score)
